package xin.alum.aim.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextUtil implements ApplicationContextAware {

    private static ApplicationContext Context;

    /**
     * 获取bean
     *
     * @param name
     * @return
     * @throws BeansException
     */
    public static Object getBean(String name) throws BeansException {
        return Context == null ? null : Context.getBean(name);
    }

    public static <T> T getBean(Class<T> c) throws BeansException {
        return Context == null ? null : Context.getBean(c);
    }

    public static ApplicationContext getApplicationContext() {
        return Context;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextUtil.Context = applicationContext;
    }
}
